package com.huqing.icu.utils;

import com.huqing.icu.exceptions.BusinessException;
import com.huqing.icu.protocol.SystemResponseCode;

import java.util.Collections;
import java.util.List;

/**
 * 分页工具类
 */
public class PageUtils {


    private static final int PAGE_SIZE = 10;

    private static final int MAX_PAGE_SIZE = 100;

    private static int getPage(Integer page) {
        if (page == null)
            page = 1;

        if (page <= 0)
            page = 1;
        return page;
    }

    public static int getPageStartIndex(Integer page) {
        page = getPage(page);
        int pageStartIndex = (page - 1) * PAGE_SIZE;
        return pageStartIndex;
    }

    public static int getPageEndIndex(Integer page) {
        int pageStartIndex = getPageStartIndex(page);
        int pageEndIndex = pageStartIndex + PAGE_SIZE;
        return pageEndIndex;
    }

    public static int getPageStartIndex(Integer page, Integer pageSize) {
        page = getPage(page);
        if (pageSize != null) {
            int pageStartIndex = (page - 1) * pageSize;
            return pageStartIndex;
        } else {
            return 0;
        }
    }

    public static int getPageSize(Integer pageSize, int defaultSize) {
        if (pageSize == null || pageSize < 0)
            return defaultSize;
        return pageSize;
    }

    public static int getPageSize(Integer pageSize) {
        if (pageSize == null || pageSize < 0 || pageSize > MAX_PAGE_SIZE) {
            return PAGE_SIZE;
        }
        return pageSize;
    }

    public static int getTotalPage(Integer totalCount, Integer pageSize) {
        if (pageSize == null) {
            return 1;
        }
        if (pageSize < 0 || pageSize == 0) {
            throw new BusinessException(SystemResponseCode.INTERNAL_ERROR.getCode(), "pageSize参数不正确:" + pageSize);
        }
        int totalPage = totalCount / pageSize;
        totalPage += totalCount % pageSize == 0 ? 0 : 1;
        return totalPage;
    }

    public static int getPageEndIndex(Integer page, Integer pageSize) {
        Integer pageStartIndex = getPageStartIndex(page, pageSize);
        if (pageSize != null) {
            Integer pageEndIndex = pageStartIndex + pageSize - 1;
            return pageEndIndex;
        } else {
            return -1;
        }
    }

    /**
     * 分割数组
     *
     * @param list     数组
     * @param page     页码
     * @param pageSize 每页数据大小
     * @return
     */
    public static <T> List<T> getPageListByPage(List<T> list, Integer page, Integer pageSize) {
        if (list == null || list.isEmpty())
            return Collections.emptyList();
        if (page == null || page <= 0)
            page = 1;
        if (pageSize == null || pageSize <= 0)
            pageSize = 10;
        Integer total = list.size();
        Integer fromIndex = (page - 1) * pageSize;
        Integer toIndex = fromIndex + pageSize;
        if (fromIndex >= total)
            return Collections.emptyList();
        if (toIndex > total)
            toIndex = total;
        return list.subList(fromIndex, toIndex);
    }

    /**
     * 分割数组
     *
     * @param list
     * @param pageStartIndex
     * @param pageEndIndex
     * @return
     */
    public static <T> List<T> getPageListByIndex(List<T> list, Integer pageStartIndex, Integer pageEndIndex) {
        if (list == null || list.isEmpty())
            return Collections.emptyList();
        Integer total = list.size();
        if (pageStartIndex == null || pageStartIndex < 0)
            pageStartIndex = 0;
        if (pageEndIndex == null || pageEndIndex < 0) {
            if (total > 10)
                pageEndIndex = 10;
            else
                pageEndIndex = total;
        }
        if (pageStartIndex > pageEndIndex)
            return Collections.emptyList();
        if (pageStartIndex >= total)
            return Collections.emptyList();
        if (pageEndIndex > total)
            pageEndIndex = total;

        return list.subList(pageStartIndex, pageEndIndex);
    }
}
